package com.jiangxiaofei.citycloud.activity.recruit;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.RetruitWorkNumAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.WorkNumBean;
import com.jiangxiaofei.citycloud.constants.IntConstants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.event.EventMessage;
import com.jiangxiaofei.citycloud.mvp.RecruitConstant;
import com.jiangxiaofei.citycloud.mvp.RecruitPersenter;
import com.jiangxiaofei.citycloud.view.FullyLinearLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 作者: JiangXfei
 * 日期: 2017/10/11 0011
 */

public class RecruitWorkNumberActivity extends BaseActivity implements
        RecruitConstant.getWorkNumView, RecruitConstant.getWorkerNumView {
    @InjectView(R.id.img_left_back)
    ImageView imgLeftBack;
    @InjectView(R.id.recruit_rb_do_work)
    RadioButton recruitRbDoWork;
    @InjectView(R.id.recruit_rb_find_work)
    RadioButton recruitRbFindWork;
    @InjectView(R.id.rg_title)
    RadioGroup rgTitle;
    @InjectView(R.id.img_right_menu)
    ImageView imgRightMenu;
    @InjectView(R.id.rl_title)
    RelativeLayout rlTitle;
    @InjectView(R.id.recy_retruit_work_num)
    RecyclerView recyRetruitWorkNum;

    RecruitConstant.getWorkerNumPersenter getWorkerNumPersenter;
    RecruitConstant.getWorkNumPersenter getWorkNumPersenter;
    private List<WorkNumBean> numList;
    private RetruitWorkNumAdapter numAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_retruit_work_num_layout);
        ButterKnife.inject(this);
        mContext = this;
        mActivity = this;
        EventBus.getDefault().register(this);
        initAdapter();
        getWorkerNumPersenter = new RecruitPersenter(mContext, (RecruitConstant.getWorkerNumView) mActivity);
        getWorkNumPersenter = new RecruitPersenter(mContext, (RecruitConstant.getWorkNumView) mActivity);
        getWorkNumPersenter.getWorkNum();
        imgRightMenu.setVisibility(View.GONE);
    }

    private void initAdapter() {
        numList = new ArrayList<>();
        numAdapter = new RetruitWorkNumAdapter(mContext, numList,code);
        FullyLinearLayoutManager layoutManager = new FullyLinearLayoutManager(mContext);
        recyRetruitWorkNum.setLayoutManager(layoutManager);
        recyRetruitWorkNum.setAdapter(numAdapter);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    /**
     * 获取附近工人数量
     *
     * @param o
     */
    @Override
    public void onGetWorkerNumSuccesss(Object o) {
        numList.clear();
        numAdapter = new RetruitWorkNumAdapter(mContext, numList,code);
        recyRetruitWorkNum.setAdapter(numAdapter);
        List<WorkNumBean> numBeanList = (List<WorkNumBean>) o;
        for (int i = 0; i < numBeanList.size(); i++) {
            for (int j = 0; j < numBeanList.size(); j++) {
                if (Integer.valueOf(numBeanList.get(i).getId()) >
                        Integer.valueOf(numBeanList.get(j).getId())) {
                    WorkNumBean bean = numBeanList.get(i);
                    WorkNumBean bean2 = numBeanList.get(j);
                    numBeanList.remove(i);
                    numBeanList.add(i, bean2);
                    numBeanList.remove(j);
                    numBeanList.add(j, bean);
                }
            }
        }
        numList.addAll(numBeanList);
        numAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetWorkNumSuccess(Object o) {
        numList.clear();
        numAdapter = new RetruitWorkNumAdapter(mContext, numList,code);
        recyRetruitWorkNum.setAdapter(numAdapter);
        List<WorkNumBean> numBeanList = (List<WorkNumBean>) o;
        for (int i = 0; i < numBeanList.size(); i++) {
            for (int j = 0; j < numBeanList.size(); j++) {
                if (Integer.valueOf(numBeanList.get(i).getId()) >
                        Integer.valueOf(numBeanList.get(j).getId())) {
                    WorkNumBean bean = numBeanList.get(i);
                    WorkNumBean bean2 = numBeanList.get(j);
                    numBeanList.remove(i);
                    numBeanList.add(i, bean2);
                    numBeanList.remove(j);
                    numBeanList.add(j, bean);
                }
            }
        }
        numList.addAll(numBeanList);
        numAdapter.notifyDataSetChanged();
    }

    int code=IntConstants.DOWORK;

    @OnClick({R.id.recruit_rb_do_work, R.id.recruit_rb_find_work,
            R.id.img_left_back})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.recruit_rb_do_work:
                //我要干活
                code = IntConstants.DOWORK;
                recruitRbFindWork.setTextColor(Color.WHITE);
                recruitRbDoWork.setTextColor(getResources().getColor(R.color.maintab_text_selected_color));
                getWorkNumPersenter.getWorkNum();
                break;
            case R.id.recruit_rb_find_work:
                //点击我需劳务
                code = IntConstants.FINDWORK;
                recruitRbDoWork.setTextColor(Color.WHITE);
                recruitRbFindWork.setTextColor(getResources().getColor(R.color.maintab_text_selected_color));
                getWorkerNumPersenter.getWorkerNum();
                break;
            case R.id.img_left_back:
                //返回
                this.finish();
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void itemOnclick(EventMessage.WorkNumItemClickMsg msg) {
        Intent intent = new Intent(mContext, RecruitMapAcitivity.class);
        intent.putExtra(StringConstants.ROLE, msg.getId());
        intent.putExtra(StringConstants.CODE, code);
        mContext.startActivity(intent);
    }

    @Override

    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
